import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

interface User {
  id: string
  name: string
  email: string
  role: string
  avatar?: string
}

export const useUserStore = defineStore('user', () => {
  const user = ref<User | null>({
    id: '1',
    name: '管理员',
    email: 'admin@test.com',
    role: 'admin'
  })
  
  const isLoggedIn = computed(() => !!user.value)

  const setUser = (userData: User) => {
    user.value = userData
  }

  const logout = () => {
    user.value = null
    localStorage.removeItem('token')
    // 这里可以添加重定向到登录页的逻辑
  }

  const updateProfile = (updates: Partial<User>) => {
    if (user.value) {
      user.value = { ...user.value, ...updates }
    }
  }

  return {
    user,
    isLoggedIn,
    setUser,
    logout,
    updateProfile
  }
})

